<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Stok_peternakan Extends CI_Controller{
	
	function Stok_peternakan()
	{
		parent::__construct();
		$this->load->model('sapi_model');
		$this->load->model('stok_peternakan_model');
		$this->load->model('stok_model');
		$this->load->model('Home_model');
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE){
			redirect('auth');
		}else{
			$id_role = $this->session->userdata('id_role'); 
			$data = array(
				"main_view" 		=> "stok_peternakan/konten",
				"title"				=> "Manajemen Stok Peternakan",
				"nama_user"			=> "Admin",
				"jenis_sapi"		=> $this->sapi_model->get_jenis_sapi(),
				"peternakan"		=> $this->sapi_model->get_peternakan(),
				"alasan"			=> $this->stok_peternakan_model->get_alasan(),
				"data_stok"			=> $this->stok_peternakan_model->get_data_stok(),
				"hak_akses0"		=> $this->Home_model->get_hak_akses($id_role,0),
				"hak_akses1"		=> $this->Home_model->get_hak_akses($id_role,1),
				"hak_akses2"		=> $this->Home_model->get_hak_akses($id_role,2)
			);
			//print_r($data['data_stok']);
			$this->load->view('template', $data);
		}
		
	}
	
	function input_history()
	{
		$tgl_berdiri = $this->input->post("tanggal-kejadian");
		$tgl = explode('/', $tgl_berdiri);
		if(count($tgl) > 1) $tgl_berdiri = $tgl[2]."-".$tgl[0]."-".$tgl[1];
		
		$input_jumlah = $this->input->post("jumlah");
		$id_alasan = $this->input->post("alasan");
		
		$alasan = $this->stok_model->get_alasan_by_id($id_alasan);
		
		if($alasan->JENIS_ALASAN == 0){ //pengurangan
			$jumlah = $input_jumlah * -1;
		}
		
		else if($alasan->JENIS_ALASAN == 1){ //penambahan
			$jumlah = $input_jumlah;
		}
		
		$stok = array(
			"ID_USER"			=> $this->session->userdata('id_user'),
			"ID_SAPI"			=> $this->input->post("sapi"),
			"ID_TITIK_PASOK"	=> $this->input->post("peternakan"),
			"JUMLAH"			=> $jumlah,
			"ALASAN_PERUBAHAN"	=> $id_alasan,
			"TANGGAL_KEJADIAN"	=> $tgl_berdiri,
			"KOMENTAR"			=> $this->input->post("catatan")
		);
		
		if($this->stok_peternakan_model->insert_history($stok)){
			redirect("stok_peternakan");
		} else {
			echo "gagal";
		}
	}
	
	
}